package base_demo.movieTest;

public class MyThread extends Thread {
    //定义统一静态变量
    static int ticket = 0;

    @Override
    public void run() {
        //1.先实现循环
        //2.编写同步代码块
        while (true) {
            synchronized (MyThread.class) {
            //判断是否卖完了
            if (ticket == 50) {
                return;
            }

                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(getName() + "卖出了第" + ticket + "张票!!!");
                }
            }

        }
    }

